home *** CD-ROM | disk | FTP | other *** search
- Path: koobera.math.uic.edu!djb
- From: djb@koobera.math.uic.edu (D. J. Bernstein)
- Message-ID: <1996Feb2300.56.01.15905@koobera.math.uic.edu>
- Date: 23 Feb 1996 00:56:01 GMT
- Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
- Subject: Re: Access carry flag from C
- References: <Dn1C9z.DGv.0.net@indra.com> <1996Feb1922.17.19.879@koobera.math.uic.edu> <4gihtk$33k@sun001.spd.dsccc.com>
- Organization: IR
-
- Mike McCarty <jmccarty@spd.dsccc.com> wrote:
- > D. J. Bernstein <djb@koobera.math.uic.edu> wrote:
- > > A smart compiler will optimize ``if (i < j)''---I'm assuming here that
- > > all variables are unsigned---into a test of the carry bit.
- > Depends on which (if any) of i and j are unsigned whether that is even
- > the correct test.
-
- Learn to read. ``I'm assuming here that all variables are unsigned.''
-
- > Also, your statement actually depends on having complement arithmetic,
-
- The correctness of ``if (i < j)'' as an overflow test is guaranteed by
- the ANSI C definition of unsigned arithmetic, which happens to match
- what every existing compiler does.
-
- On all the CPUs I care about, and presumably all the CPUs that the
- original poster cares about, there's a carry bit equalling (i < j). A
- good compiler will recognize this; an excellent compiler will document
- that fact; a bad compiler will do an extra subtraction, which at least
- isn't painfully slow.
-
- [ signed arithmetic ]
- > This is a difficult problem, and such checks are going to be s-l-o-w.
-
- There's no reason for C to be any slower than asm. If the compiler
- provides a documented idiom for each machine-language operation, you can
- achieve the speed of asm with no loss in portability.
-
- ---Dan
-